package com.ha8.netease.views.team.bloglist
{
	import com.ha8.netease.App;
	import com.ha8.netease.PrivateNotify;
	import com.ha8.netease.models.WeiboProxy;
	import com.ha8.netease.models.vo.FavReqVO;
	import com.ha8.netease.models.vo.MainReqVO;
	import com.ha8.netease.models.vo.MentionReqVO;
	import com.ha8.netease.models.vo.MicroblogModel;
	import com.ha8.netease.models.vo.ViewModel;
	import com.ha8.netease.views.utils.DataUtil;
	import com.ha8.netease.views.utils.SortUtil;
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * 主页视图层代理
	 * 
	 * 主页 收藏 @提到我的
	 * 
	 * @author rogo
	 */	
	public class BloglistMediator extends Mediator
	{
		public function BloglistMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'blogListMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var model:ViewModel;
		private var mainVO:MainReqVO;
		private var favVO:FavReqVO;
		private var mentionVO:MentionReqVO;
		private var since_id:String = null;
		private var max_id:String = null;
		private var isloading:Boolean = false;
		
		/**
		 * 视图显示的微博数组
		 */
		private var displayMicroblogs:ArrayCollection;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			displayMicroblogs = new ArrayCollection();
			displayMicroblogs.sort = SortUtil.getMicroblogSort();
			view.dataGroup.dataProvider = displayMicroblogs;
			
			view.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			view.scroller.verticalScrollBar.addEventListener(Event.CHANGE, scroller_changeHandler);
			
			displayUser();
		}
		
		override public function listNotificationInterests():Array
		{
			return [PrivateNotify.GET_USERTIMELINE_RESULT, 
				PrivateNotify.GET_USERTIMELINE_ERROR,
				PrivateNotify.GET_USERFAVORITE_RESULT,
				PrivateNotify.GET_USERFAVORITE_ERROR,
				PrivateNotify.GET_USERMENTION_RESULT,
				PrivateNotify.GET_USERMENTION_ERROR];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case PrivateNotify.GET_USERMENTION_RESULT:
				case PrivateNotify.GET_USERFAVORITE_RESULT:
				case PrivateNotify.GET_USERTIMELINE_RESULT:
					updateData(notification.param as ArrayCollection);
					isloading = false;
					break;
				case PrivateNotify.GET_USERFAVORITE_ERROR:
				case PrivateNotify.GET_USERTIMELINE_ERROR:
				case PrivateNotify.GET_USERMENTION_ERROR:
					isloading = false;
					break;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():BlogListView
		{
			return viewComponent as BlogListView;
		}
		
		public function get proxy():WeiboProxy
		{
			return facade.retrieveProxy(WeiboProxy.NAME) as WeiboProxy;
		}
		
		/**
		 * 显示用户数据
		 */		
		public function displayUser():void
		{
			if (model &&
				model.equal(App.getInstance().blogListModel))
				return;
			
			model = App.getInstance().blogListModel;
			isloading = false;
			displayMicroblogs.removeAll();
			
			switch (model.type)
			{
				case ViewModel.MAIN:
					mainVO = new MainReqVO();
					mainVO.user_id = model.user.id;
					if (model.user.id == App.getInstance().me.id)
						view.title = '我的主页';
					else
						view.title = model.user.name + '的主页';
					break;
				case ViewModel.FAVORITE:
					favVO = new FavReqVO();
					favVO.id = model.user.screen_name;
					if (model.user.id == App.getInstance().me.id)
						view.title = '我的收藏';
					else
						view.title = model.user.name + '的收藏';
					break;
				case ViewModel.MENTION:
					mentionVO = new MentionReqVO();
					view.title = '@提到我的';
					break;
			}
			
			requestData();
		}
		
		/**
		 * 更新数据
		 */		
		public function updateData(data:ArrayCollection):void
		{
			if (!data || data.length == 0)
				return;
			
			DataUtil.merge(displayMicroblogs, data);
			displayMicroblogs.refresh();
			
			// 记录游标
			if (displayMicroblogs.length > 0)
			{
				max_id = MicroblogModel(displayMicroblogs[0]).cursor_id;
				since_id = MicroblogModel(displayMicroblogs[displayMicroblogs.length - 1]).cursor_id;
			}
		}
		
		/**
		 * 开始请求数据
		 */		
		private function requestData():void
		{
			if (isloading)
				return;
			
			switch (model.type)
			{
				case ViewModel.MAIN:
					proxy.getMainTimeline(mainVO);
					break;
				case ViewModel.FAVORITE:
					proxy.getFavorite(favVO);
					break;
				case ViewModel.MENTION:
					proxy.getMention(mentionVO);
					break;
			}
			
			isloading = true;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function addToStageHandler(e:Event):void
		{
			displayUser();	
		}
		
		private function scroller_changeHandler(e:Event):void
		{
			// 滚到最底部 获取更多数据
			if (view.dataGroup.verticalScrollPosition >= 
				view.scroller.verticalScrollBar.maximum)
			{
				switch (model.type)
				{
					case ViewModel.MAIN:
					{
						// 获取since_id之前发的微博列表
						mainVO.since_id = since_id;
						mainVO.max_id = null;
						break;
					}
					case ViewModel.FAVORITE:
					{
						favVO.since_id = since_id;
						break;
					}
					case ViewModel.MENTION:
					{
						mentionVO.since_id = since_id;
						mentionVO.max_id = null;
					}
				}
				requestData();
			}
		}
		
	}
}